package com.google.appengine.api.datastore;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * @author Secondary User
 */
public class Entity
{
	//Need to ensure that we can create the ID part of the key during the save.

	public static String KEY_RESERVED_PROPERTY = "__key_reserved_property__";
	private Map<String, Object> data = new HashMap<String, Object>();
	private Set<String> toIndex = new HashSet<String>();
	@Getter
	@Setter
	private Key key;
	private Key parent;
	private String kind;

	public Entity(String kind, Key parent)
	{
		this.kind = kind;
		this.parent = parent;
	}

	public Entity(Key key)
	{
		this.key = key;
	}

	public Entity(String kind)
	{
		this.kind = kind;
		this.parent = null;
	}
	
	Key getParent()
	{
		if (key == null)
			return parent;
		else
			return key.getParent();
	}

	public Object getProperty(String name)
	{
		return data.get(name);
	}

	public void setUnindexedProperty(String name, Object value)
	{
		data.put(name, value);
	}

	public void setProperty(String name, Object value)
	{
		data.put(name, value);
		toIndex.add(name);
	}

	public Map<String, Object> getIndexedProperties()
	{
		Map<String, Object> toReturn = new HashMap<String, Object>();
		for (String property : toIndex)
		{
			toReturn.put(property, data.get(property));
		}
		return toReturn;
	}

	public boolean hasProperty(String name)
	{
		return data.containsKey(name);
	}

	public Map<String, Object> getProperties()
	{
		return Collections.unmodifiableMap(data);
	}

	public String getKind()
	{
		if (key == null)
		{
			return kind;
		}
		else
		{
			return key.getKind();
		}
	}
}
